package cn.hxth.cloud.provider.service.impl;

import cn.hxth.cloud.api.common.util.AbstractBaseServiceImpl;
import cn.hxth.cloud.api.common.util.DateProvider;
import cn.hxth.cloud.api.domain.*;
import cn.hxth.cloud.api.mapper.*;
import cn.hxth.cloud.api.rabbitmq.RabbitMQSender;
import cn.hxth.cloud.provider.service.WmsService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WmsServiceImpl extends AbstractBaseServiceImpl implements WmsService {
    private Logger logger = LoggerFactory.getLogger(WmsServiceImpl.class);
    @Autowired
    private ProductOutputOrderMapper productOutputOrderMapper;
    @Autowired
    private ProductOutputOrderItemMapper productOutputOrderItemMapper;
    @Autowired
    private SteelsInputOrderMapper steelsInputOrderMapper;
    @Autowired
    private SteelsInputOrderItemMapper steelsInputOrderItemMapper;
    @Autowired
    private SteelsOutputOrderMapper steelsOutputOrderMapper;
    @Autowired
    private ProductInputOrderMapper productInputOrderMapper;
    @Autowired
    private ProductInputOrderItemMapper productInputOrderItemMapper;

    @Autowired
    public WmsServiceImpl(RabbitMQSender rabbitMQSender, RedisTemplate<String, String> redisTemplate, DateProvider dateProvider) {
        super(rabbitMQSender, redisTemplate, dateProvider);
    }

    @Override
    public Boolean insertProductOutputOrder(ProductOutputOrder productOutputOrder) {
        try {
            this.productOutputOrderMapper.insertSelective(productOutputOrder);
            for (ProductOutputOrderItem item : productOutputOrder.getProductOutputOrderItems()) {
                item.setOid(productOutputOrder.getId());
            }
            productOutputOrderItemMapper.insertList(productOutputOrder.getProductOutputOrderItems());
            super.sendMessage("inventory", productOutputOrder, "updateProductOutputInventory", null);
        } catch (Exception e) {
            logger.error("error", e);
            return false;
        }
        return true;
    }

    @Override
    public Boolean insertProductInputOrder(ProductInputOrder productInputOrder) {
        Map<Long, ProductInputOrderItem> productInputOrderItemMap = null;
        try {
            productInputOrder.setMakeDate(dateProvider.getCurrentTime());
            String jsonData = redisTemplate.opsForValue().get("productInputOrderItem");
            if (jsonData != null) {
                productInputOrderItemMap = JSONObject.parseObject(jsonData, new TypeReference<Map<Long, ProductInputOrderItem>>() {
                });
                List<ProductInputOrderItem> productInputOrderItems = new ArrayList<>(productInputOrderItemMap.values()) ;
                productInputOrder.getProductInputOrderItems().addAll(productInputOrderItems);
                for (ProductInputOrderItem productInputOrderItem : productInputOrderItems) {
                    productInputOrderItem.setOid(productInputOrder.getId());
                }
            }

            this.productInputOrderItemMapper.insertList(productInputOrder.getProductInputOrderItems());
            super.sendMessage("inventory", productInputOrder, "updateProductInputInventory", null);
        } catch (Exception e) {
            logger.error("error", e);
            return false;
        }
        return true;

    }

    public Boolean insertSteelsInputOrder(String mid) {
        try {
            SteelsInputOrder steelsInputOrder = new SteelsInputOrder();
            steelsInputOrder.setMid(mid);
            steelsInputOrder.setMakeDate(dateProvider.getCurrentTime());
            steelsInputOrder.setTotalcount(100000.0);
            this.steelsInputOrderMapper.insertSelective(steelsInputOrder);
            String jsonData = this.redisTemplate.opsForValue().get("steelsInputOrderItem");
            System.out.println(jsonData);
            Map<Long, SteelsInputOrderItem> steelsInputOrderItemMap = JSONObject.parseObject(jsonData, new TypeReference<Map<Long, SteelsInputOrderItem>>() {
            });
            List<SteelsInputOrderItem> steelsInputOrderItemList = new ArrayList<>(steelsInputOrderItemMap.values()) ;
            for (SteelsInputOrderItem steelsInputOrderItem : steelsInputOrderItemList) {
                steelsInputOrderItem.setOid(steelsInputOrder.getId());
            }
            steelsInputOrder.setSteelsInputOrderItems(steelsInputOrderItemList);
            this.steelsInputOrderItemMapper.insertList(steelsInputOrderItemList);

            super.sendMessage("inventory", steelsInputOrder, "updateSteelsInputInventory", mid);
        } catch (Exception e) {
            logger.error("error", e);
            return false;
        }
        return true;
    }

    @Override
    public Boolean insertSteelsOutputOrder(SteelsOutputOrder steelsOutputOrder) {

        try {
            this.steelsOutputOrderMapper.insertSelective(steelsOutputOrder);
            super.sendMessage("inventory", steelsOutputOrder, "updateSteelsOutputInventory", null);

        } catch (Exception e) {
            logger.error("error", e);
            return false;
        }
        return true;
    }


}

